# cut -f1 -d " " BRCA_fwd.tfam |cut -f4 -d '-'
$dir = $ARGV[0];
chomp($dir);
$input_tfam = $ARGV[1];
chomp($input_tfam);
$flag  = $ARGV[2];
chomp($flag);

open(WRBUFF1,">$dir/tumor.txt");
open(WRBUFF2,">$dir/normal.txt");
print $flag."\n";
if($flag eq "binary"){
open BUFF,"$dir/$input_tfam.fam" or die "no file $dir/$input_tfam.fam\n";
}
else
{
open BUFF,"$dir/$input_tfam.tfam" or die "no file $dir/$input_tfam.tfam\n";
}
while($line=<BUFF>)
{
	chomp($line);
	@array = split(" ",$line);
	@array1 = split('-',$array[1]);
	#print $array1[3]."\n";
	@array2 = split(//,$array1[3]);
	$l = $array2[0].$array2[1];
	if($l eq "01")
	{
		print WRBUFF1 $line."\n";
	}
	elsif($l eq "10" || $l eq "11" || $l eq "12")
	{
		print WRBUFF2 $line."\n";
	}
	else
	{
	}
	
	#print $array2[0].$array2[1]."\n";
}
if($flag eq "binary"){
$infile = "bfile";
}
else
{
$infile = "tfile";
}
print $flag."\t".$infile."\n";
system("grep -P \"^0\"  $dir/$input_tfam.tped |cut -f2 -d ' ' >$dir/$input_tfam-removedsnp_nochr.txt");
$line = "/data4/bsi/RandD/Workflow/genotyping_QC_FWD/bin/plink/plink --$infile $dir/$input_tfam --keep $dir/tumor.txt --exclude  $dir/$input_tfam-removedsnp_nochr.txt --recode --transpose --out $dir/$input_tfam"."_tumor";

system($line);
$line = "/data4/bsi/RandD/Workflow/genotyping_QC_FWD/bin/plink/plink --$infile $dir/$input_tfam --keep $dir/normal.txt --exclude  $dir/$input_tfam-removedsnp_nochr.txt --recode --transpose --out $dir/$input_tfam"."_normal";
system($line);

